{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python_scet",
   "display_name": "scet"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'hello userxxx'"
     },
     "metadata": {},
     "execution_count": 1
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "url = 'http://127.0.0.1:5000/user/userxxx'\n",
    "r = requests.get(url)\n",
    "r.text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'success'"
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "# URL路径参数\n",
    "url = 'http://127.0.0.1:5000/data?a=11&b=22'\n",
    "r = requests.get(url)\n",
    "r.text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'success'"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "# 通过构造json参数提交请求\n",
    "url = 'http://127.0.0.1:5000/data_json'\n",
    "data = json.dumps({'name': 'zkd', 'age': 26})\n",
    "\n",
    "r = requests.get(url, data=data)\n",
    "r.text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'success'"
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "# 通过构造cookie提交请求\n",
    "url = 'http://127.0.0.1:5000/data_cookie'\n",
    "data = json.dumps({'name': 'zhangsan', 'age': 26})\n",
    "cookie = {'token': 'tokenXXX'}\n",
    "\n",
    "r = requests.get(url, data=data, cookies=cookie)\n",
    "r.text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "200\n[[\"2019/7/1\", \"1000\", \"10\"], [\"2019/7/2\", \"2000\", \"20\"], [\"2019/7/3\", \"3000\", \"30\"], [\"2019/7/4\", \"4000\", \"40\"]]\n[['2019/7/1', '1000', '10'], ['2019/7/2', '2000', '20'], ['2019/7/3', '3000', '30'], ['2019/7/4', '4000', '40']]\n"
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "# 模拟前端获取json数据\n",
    "url = 'http://127.0.0.1:5000/getjson'\n",
    "r = requests.get(url)\n",
    "\n",
    "print(r.status_code)\n",
    "print(r.text)\n",
    "\n",
    "print(json.loads(r.text))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sqlite3\n",
    "\n",
    "con = sqlite3.connect('./data/userTest.db')\n",
    "cur = con.cursor()\n",
    "\n",
    "cur.execute('drop table if exists user;')\n",
    "cur.execute(\n",
    "    '''\n",
    "    create table user (\n",
    "        id INTEGER PRIMARY KEY   AUTOINCREMENT,\n",
    "        name TEXT  default '',\n",
    "        sex char(10) default '',\n",
    "        age int default '0',\n",
    "        email TEXT default ''\n",
    "    );\n",
    "    '''\n",
    ")\n",
    "\n",
    "con.commit()\n",
    "con.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "((('id', None, None, None, None, None, None),\n  ('name', None, None, None, None, None, None),\n  ('sex', None, None, None, None, None, None),\n  ('age', None, None, None, None, None, None),\n  ('email', None, None, None, None, None, None)),\n [(1, 'zhangsan', '男', 23, 'xxx@qq.com')])"
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "source": [
    "import sqlite3\n",
    "\n",
    "con = sqlite3.connect('./data/userTest.db')\n",
    "cur = con.cursor()\n",
    "cur.execute('select * from user;')\n",
    "cur.description, cur.fetchall()"
   ]
  }
 ]
}